package com.aggron.concurrency.commonunsafe;

import com.aggron.concurrency.annotations.NotRecommend;
import com.aggron.concurrency.annotations.NotThreadSafe;
import com.aggron.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @author LR2SD 线程封闭的写法（堆栈封闭）
 * @description SimpleDateFormat
 * @date 2018-06-11 19:17
 */
@Slf4j
@ThreadSafe
@NotRecommend
public class DateFormatExample2 {
    //public static   SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
    /**
     * 请求总数
     */
    public static  int clientTotal=5000;
    /**
     * 同时执行线程的总数
     */
    public static  int threadTotal =200;



    public static void main(String [] args) throws Exception {
        //线程池
        ExecutorService executorService= Executors.newCachedThreadPool();
        //信号量--允许的最大并发量
        final Semaphore semaphore=new Semaphore(threadTotal);

        //阻塞当前线程，直至线程减少到0时
        final CountDownLatch countDownLatch=new CountDownLatch(clientTotal);

        for (int i = 0; i < clientTotal; i++) {
            executorService.execute(()->{
                try {
                    semaphore.acquire();
                    add();
                    semaphore.release();
                } catch (Exception e) {
                    log.error(e.getMessage(),e);
                }
                countDownLatch.countDown();

            });
        }
        countDownLatch.await();
        executorService.shutdown();

    }

    private static void add(){
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
            simpleDateFormat.parse("20180611");
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
        }

    }
}
